package com.dsg.service.oil.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.*;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.entity.dto.NewCardDto;
import com.dsg.entity.dto.RenewCardDto;
import com.dsg.entity.dto.ReplaceCardImportDto;
import com.dsg.entity.oil.DsgOilCardStock;
import com.dsg.dao.oil.DsgOilCardStockMapper;
import com.dsg.service.oil.IDsgOilCardStockService;
import com.dsg.service.DsgServiceImpl;
import com.dsg.util.ExcelIteratorReader;
import com.dsg.util.verify.VerifyHolder;
import com.dsg.util.verify.impl.NotEmptyVerify;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 油咭庫存 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-28
 */
@Service
public class DsgOilCardStockServiceImpl extends DsgServiceImpl<DsgOilCardStockMapper, DsgOilCardStock> implements IDsgOilCardStockService {

    @Override
    public AjaxResult importExcel(File file, Integer returnType) throws Exception {
        AjaxResult result = new AjaxResult(MessageConstants.SSO_STATUS_SUCCESS);
        List<String> errorList = new ArrayList<>();
        ExcelIteratorReader excelIteratorReader = null;
        VerifyHolder verifyHolder = null;
        switch (returnType) {
            case DsgOilCardStock.RETURN_TYPE_1:
                excelIteratorReader = new ExcelIteratorReader(file, ReplaceCardImportDto.class, 0, 0, errorList);
                verifyHolder = new VerifyHolder(ReplaceCardImportDto.class, errorList);
                break;
            case DsgOilCardStock.RETURN_TYPE_2:
                excelIteratorReader = new ExcelIteratorReader(file, NewCardDto.class, 0, 0, errorList);
                verifyHolder = new VerifyHolder(NewCardDto.class, errorList);
                break;
            case DsgOilCardStock.RETURN_TYPE_3:
                excelIteratorReader = new ExcelIteratorReader(file, RenewCardDto.class, 0, 0, errorList);
                verifyHolder = new VerifyHolder(RenewCardDto.class, errorList);
                break;
        }
        List<DsgOilCardStock> addList = new ArrayList();
        verifyHolder.addVerify(new NotEmptyVerify());
        int rowIdx = 1;
        Iterator iterator = excelIteratorReader.iterator();
        if (iterator.hasNext()) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.select("CARD_NO");
            wrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
            List<DsgOilCardStock> existStockList = list(wrapper);
            List<String> cardNoList = new ArrayList<>();

            while (iterator.hasNext()) {
                rowIdx++;
                Object obj = iterator.next();
                verifyHolder.check(obj, rowIdx);

                String cardNo = (String) BeanKit.getProperty(obj, "cardNo");
                DsgOilCardStock s = existStockList.stream().filter(e -> cardNo.equals(e.getCardNo())).findFirst().orElse(null);
                if (s != null) {
                    errorList.add(String.format("第%s行的油咭號已存在油咭庫存中", rowIdx));
                }
                if (cardNoList.contains(cardNo)) {
                    errorList.add(String.format("第%s行的油咭號重覆", rowIdx));
                }
                cardNoList.add(cardNo);
                if (CollectionUtil.isEmpty(errorList)) {
                    DsgOilCardStock stock = new DsgOilCardStock();
                    BeanUtils.copyProperties(stock, obj);
                    addList.add(stock);
                }
            }
        }

        if (CollectionUtil.isNotEmpty(errorList)) {
            result = new AjaxResult(MessageConstants.SSO_STATUS_FAIL);
            result.setMessage(errorList.toString());
            return result;
        } else {
            if (CollectionUtil.isNotEmpty(addList)) {
                addList.forEach(e -> {
                    e.setOid(UUIDGenerator.getUUID());
                    e.setStockStatus(DsgOilCardStock.STOCK_STATUS_IMPORT);
                    e.setReturnType(returnType);
                });
                saveBatch(addList);
            } else {
                result = new AjaxResult(MessageConstants.SSO_STATUS_FAIL);
                result.setMessage("導入數據為空");
                return result;
            }
        }

        return result;
    }
}
